import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:wallpaper/api/author.dart';
import 'package:wallpaper/components/placeholder/appbar_base.dart';
import 'package:wallpaper/generated/l10n.dart';
import 'package:wallpaper/tools/update_apk.dart';

class AboutPage extends StatefulWidget {
  const AboutPage({super.key});

  @override
  State<AboutPage> createState() => _AboutPageState();
}

class _AboutPageState extends State<AboutPage> {
  String version = '';
  List logs = [];

  Future<void> getVersion() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    setState(() {
      version = packageInfo.version;
    });
  }

  Future<void> getUpdateLogs() async {
    Response res = await updateApp();
    setState(() {
      logs = res.data['update'];
    });
  }

  @override
  void initState() {
    super.initState();
    getVersion();
    getUpdateLogs();
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context).colorScheme;
    final primary = theme.primary;
    return Scaffold(
      appBar: AppbarBase(title: S.of(context).about),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: ListView(
          children: [
            Material(
                color: theme.primaryContainer,
                borderRadius: BorderRadius.circular(8),
                clipBehavior: Clip.antiAlias,
                child: ListTile(
                  title: Text(S.of(context).s18),
                  trailing: Text('v$version'),
                )),
            SizedBox(height: 8),
            Material(
                color: theme.primaryContainer,
                borderRadius: BorderRadius.circular(8),
                clipBehavior: Clip.antiAlias,
                child: ListTile(
                  title: Text(S.of(context).s19),
                  onTap: () {
                    UpdateApk().updateApk(context, displayTips: true);
                  },
                )),
            SizedBox(height: 8),
            Material(
                color: theme.primaryContainer,
                borderRadius: BorderRadius.circular(8),
                clipBehavior: Clip.antiAlias,
                child: ListTile(
                  title: Text('官方网站'),
                  subtitle: Text('github托管，有时需要翻墙才能访问'),
                  onTap: () async {
                    var url =
                        Uri.parse('https://zsnoin.github.io/bocchi_wallpaper/');
                    if (!await launchUrl(url)) throw 'Could not launch $url';
                  },
                )),
            SizedBox(height: 8),
            // 更新日志
            Row(
              spacing: 5,
              children: [
                SizedBox(width: 0),
                Icon(Icons.new_releases_sharp, color: primary),
                Text(
                  '更新日志',
                  style: TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                    color: primary,
                  ),
                ),
              ],
            ),
            Padding(
              padding: const EdgeInsets.all(10.0),
              child: ListView.separated(
                shrinkWrap: true,
                itemCount: logs.length,
                physics: NeverScrollableScrollPhysics(),
                separatorBuilder: (context, idx) => SizedBox(height: 6),
                itemBuilder: (context, index) {
                  return Text(
                    '${index + 1}. ${logs[index]}',
                    style: TextStyle(fontSize: 15),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
